#include <yatak/debug.h>
#include <yatak/kheap.h>
#include <libc/stdio.h>
#include <keyboard.h>
#include <string.h>
#include <types.h>
#include <shell.h>
#include <sched.h>

extern const char * __comm_arr [COMM_ARR_SIZE] [2];

int exec_line (char * com)
{
	char * param = (char *) kmalloc(sizeof(char)*256);
	char * sparam = param;
	char * line;
	int i;
	if (com [0] == '\0')
	{
		kfree (param);
		return 1;
	}
	// Find commands
	strcpy (param, com);
	line = strsep (&param," ");
	if (param)
	{
		if (!strcmp (line, "if"))
		{
			line = strsep (&param,"(");
			line = strsep (&param,")");
			param = line;
			//line = strsep (&param,"==");
		}
		for (i = 0; i < COMM_ARR_SIZE; i++)
		{
			if (!strcmp (__comm_arr[i][0],line))
			{
				start_task(line,false,__comm_arr[i][1],1,param);
				kfree (sparam);
				return 0;
			}
		}
		kfree (sparam);
		return 0;
	}
	param = line;
	line = strsep (&param,"=");
	if (param)
	{
		if (!strcmp (param, "read"))
		{
			char * str [256] = {'\0'};
			in_string (str, 256);
			if (CreateVariable (line, (void *) str,sizeof(str)))
				dprintf ("[shell_interp] Cannot create variable...\n");
		}
		else if (param[0] == '\"')
		{
			char * tmpline = strsep (&param,"\"");
			tmpline = strsep (&param,"\"");
			param = tmpline;
			if (CreateVariable (line, (void *) param,(sizeof(param)*strlen(param))))
				dprintf ("[shell_interp] Cannot create variable...\n");
		}
		else
		{
			if (CreateVariable (line, (void *) atoi(param),sizeof(uint32)))
				dprintf ("[shell_interp] Cannot create variable...\n");
		}
		kfree (sparam);
		return 0;
	}
	
	for (i = 0; i < COMM_ARR_SIZE; i++)
	{
		if (!strcmp (__comm_arr[i][0],line))
		{
			start_task(line,false,__comm_arr[i][1],0,NULL);
			kfree (sparam);
			return 0;
		}
	}
	
	if (line[0] == '.' && line[1] == '/')
		executive_line (line);
	else
	{
		kfree (sparam);
		return 2;
	}
	kfree (sparam);
	return 0;
}

// Сейчас еще передаю размер массива, после, когда буду читать файл это не понадобится!
int shell_interp (fs_node_t * file)
{
	char buf [256];
	int seek = 0, sread;
	sread = fread_line(file, 0, 256, buf);
	seek += sread+1;
	if (!sread || strcmp (buf,"!./yatak/interp"))
	{
		dprintf ("[shell_interp] Incorrect type of file!!!\n");
		return 1;
	}
	
	do
	{
		sread = fread_line(file, seek, 256, buf);
		dprintf ("[shell_interp] seek = %d, sread = %d, buf = %s...\n", seek, sread, buf);
		
		if (!sread)
		{
			uint32 sz = read_fs(file, seek, 256, buf);
			if (sz)
			{
				sread = 1;
				seek += sread;
				continue;
			}
		}
		seek += sread+1;
		if (sread)
		{
			char * param = (char *) kmalloc(sizeof(char)*256);
			char * sparam = param;
			strcpy (param, buf);
			// Check that this line is coment
			char * line = strsep (&param,"#");
			int level = 0;
			if (line [0] == '\0')
			{
				kfree (sparam);
				continue;
			}
			//dprintf ("[shell_interp] line = %s...\n", line);
			//for (;;);
			// Find level code
			param = line;
			do
			{
				line = strsep (&param,"\t");
				level++;
			}
			while (param);
		
			// If empty line
			if (line [0] == '\0')
			{
				kfree (sparam);
				continue;
			}
			
			exec_line (line);
			kfree (sparam);
		}
	}
	while (sread);
	
	return 0;
}
